package com.ruoyi.oos.web.controller.startstop;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oos.startstop.domain.StartstopCount;
import com.ruoyi.oos.startstop.domain.StartstopRecord;
import com.ruoyi.oos.startstop.service.IStartstopRecordService;
import com.ruoyi.oos.tag.service.ITagGroupService;

/**
 * 设备启停统计Controller
 * 
 * @author bulang
 * @date 2020-05-13
 */
@Controller
@RequestMapping("/startstop/count")
public class StartstopCountController extends BaseController
{
    private String prefix = "startstop/count";

    @Autowired
    private IStartstopRecordService startstopRecordService;

    @Autowired
    private ITagGroupService tagGroupService;

    @RequiresPermissions("startstop:count:view")
    @GetMapping()
    public String record(ModelMap map)
    {
    		StartstopRecord startstopRecord = new StartstopRecord();
    		startstopRecord.setStartDate(DateUtils.getFirstDayOfMonth());
	  		startstopRecord.setEndDate(DateUtils.getDate());
    		
    		map.put("data", startstopRecord);
    		
        return prefix + "/count";
    }

        /**
     * 查询设备启停记录列表
     */
    @RequiresPermissions("startstop:count:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StartstopRecord startstopRecord)
    {
        // 时间查询条件处理
        String startDate = startstopRecord.getStartDate();
        if (StringUtils.isNotEmpty(startDate)) {
        	startstopRecord.setStartDate(startDate + " 00:00:00");
        }
        String endDate = startstopRecord.getEndDate();
        if (StringUtils.isNotEmpty(endDate)) {
        	startstopRecord.setEndDate(endDate + " 23:59:59");
        }
        if (startstopRecord.getGroupid() != null) {
        	List<Long> list = tagGroupService.selectSubTagGroupById(startstopRecord.getGroupid());
        	startstopRecord.setGroupIds(list);
        }

        startPage();
        
        List<StartstopCount> list = startstopRecordService.selectCountList(startstopRecord);
        return getDataTable(list);
    }

    /**
     * 导出设备启停记录列表
     */
    @RequiresPermissions("startstop:count:export")
    @Log(title = "设备启停记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StartstopRecord startstopRecord)
    {
        List<StartstopCount> list = startstopRecordService.selectCountList(startstopRecord);
        ExcelUtil<StartstopCount> util = new ExcelUtil<StartstopCount>(StartstopCount.class);
        return util.exportExcel(list, "设备启停统计");
    }
}
